<template>
	<div>
		姓名<input type="text" v-model ="obj.name"/>
		年龄<input type="text" v-model ="obj.age"/>
		男<input type="radio" name ="sex" value="男" v-model="obj.sex"/>
		女<input type="radio" name ="sex" value="女" v-model="obj.sex"/>
		<button @click="add()">添加</button>
	</div>
</template>

<script>
	export default{
		name:"Test",
		props:["obj"],
		data:function(){
			return{
				name:this.obj==null?"":this.obj.name,
				age:this.obj==null?"":this.obj.age,
				sex:this.obj==null?"":this.obj.sex,
				id:(function*id(){
					let id = 1;
					while (true){
					yield id++;
					}
				})()
			}
		},
		methods:{
			getid(){
				return this.id.next().value;
			},
			add() {
					if(this.person==null){
						let id = this.getid();
						let newperson = {
							id,
							name:this.name,
							age:this.age,
							sex:this.sex
						};
						this.$emit("getdate",newperson);
					
					}else{
						this.obj.name=this.name;
						this.obj.age=this.age;
						this.obj.sex=this.sex;
						this.$emit("finish");
					}
						this.name="";
						this.age="";
						this.sex=""	;
			},
		}
		
	}
</script>

<style>
</style>